/*
 * autor: Marcin Formela
*/


package pl.formit04.mail;


import com.google.appengine.api.datastore.Key;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class EmailStore {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String email;

    @Persistent
    private int idSeq;
    
    public EmailStore(String email, int idSeq) {
        this.email = email;
        this.idSeq = idSeq;
    }

    public static String getOwner(PersistenceManager pm, int id){
    	
    	Query queryOwner = pm.newQuery(EmailStore.class);
    	queryOwner.setFilter("idSeq == :idSeqParam");
    	String ret = null;
    	try
    	{
//    		System.out.println("1");
//    		System.out.println(id);
            List<EmailStore> results = (List<EmailStore>) queryOwner.execute(id);
//            System.out.println("3");
//            System.out.println(results.toString());
            ret = results.get(0).getEmail();
//            System.out.println("4");
    	}
    	finally
    	{
    		pm.flush();
    	}
    	return ret;
    }
    public static List<String> getCC(PersistenceManager pm, String owner){
    	
    	Query queryOwner = pm.newQuery(EmailStore.class);
    	List<String> results = new ArrayList<String>(); 
    	try
    	{
   
            List<EmailStore> resultCc = (List<EmailStore>) queryOwner.execute();
            for(EmailStore emailStore : resultCc)
            {
            	if(!(emailStore.getEmail()).equals(owner))
            	{
            		results.add(emailStore.getEmail());
            	}
            }
            

    	}
    	finally
    	{
    		pm.flush();
    	}
    	return results;
    }
    
    
    public Key getKey() {
        return key;
    }
    public String getEmail() {
        return email;
    } 
    public void setEmail(String email){
    	this.email = email;
    }
    public int getIdSeq() {
        return idSeq;
    } 
    public void setIdSeq(int idSeq){
    	this.idSeq = idSeq;
    }
}